import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;

@WebServlet("/session3")
public class session3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
           /*
           session中有一个方法:String getId()
           获取服务器为session生成的id号
         */
        String id = session.getId();

        Cookie cookie = new Cookie("JSESSIONID", id);

        cookie.setMaxAge(30);

        response.addCookie(cookie);

        session.setAttribute("username","liudadan");



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}